package com.qk.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.Course;
import com.qk.entity.Customer;
import com.qk.enums.FieldEnum;
import com.qk.enums.ParamEnum;
import com.qk.exception.CommonException;
import com.qk.mapper.CourseMapper;
import com.qk.service.BusinessService;
import com.qk.service.ClueService;
import com.qk.service.CourseService;
import com.qk.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: zhangchuang
 * @Date: 2025/10/13 08:18
 * @Description: 课程服务实现类
 */
@Service
@SuppressWarnings("all")
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ClueService clueService;

    @Autowired
    private BusinessService businessService;

    @Autowired
    private CustomerService customerService;

    @Override
    public List<Course> getBySubject(Integer subject) {
        if (ObjectUtil.isEmpty(subject)) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        return list(Wrappers.<Course>lambdaQuery().eq(Course::getSubject, subject).orderByDesc(Course::getUpdateTime));
    }

    @Override
    public boolean removeCourseById(Integer id) {
        Course c = getById(id);
        if(ObjectUtil.isEmpty(c)) {
            CommonException.throwException(ParamEnum.PARAM_ERROR);
        }
        List<Clue> l1 = clueService.list(Wrappers.<Clue>lambdaQuery().eq(Clue::getSubject, c.getSubject()));
        if (!ObjectUtil.isEmpty(l1)) {
            CommonException.throwException(FieldEnum.Field_NO_DELETE_USED_BY_CLUE);
        }
        List<Business> l2 = businessService.list(Wrappers.<Business>lambdaQuery().eq(Business::getSubject, c.getSubject()));
        if (!ObjectUtil.isEmpty(l2)) {
            CommonException.throwException(FieldEnum.Field_NO_DELETE_USED_BY_BUSINESS);
        }
        List<Customer> l3 = customerService.list(Wrappers.<Customer>lambdaQuery().eq(Customer::getSubject, c.getId()));
        if (!ObjectUtil.isEmpty(l3)) {
            CommonException.throwException(FieldEnum.Field_NO_DELETE_USED_BY_CUSTOMER);
        }
        return removeById(id);
    }

    @Override
    public boolean updateCourseById(Course course) {
        if (BeanUtil.hasNullField(course, "description", "updateTime")) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        course.setUpdateTime(LocalDateTime.now());
        return updateById(course);
    }

    @Override
    public boolean addCourse(Course course) {
        if (BeanUtil.hasNullField(course, "id", "description", "updateTime", "createTime")) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        return save(course);
    }

    @Override
    public PageResult<Course> getCourseByPage(CourseDTO courseDTO) {
        Page<Course> list = page(
                new Page<>(courseDTO.getPage(), courseDTO.getPageSize()),
                Wrappers.<Course>lambdaQuery().like(StrUtil.isNotBlank(courseDTO.getName()), Course::getName, courseDTO.getName())
                        .eq(ObjectUtil.isNotEmpty(courseDTO.getSubject()), Course::getSubject, courseDTO.getSubject())
                        .eq(ObjectUtil.isNotEmpty(courseDTO.getTarget()), Course::getTarget, courseDTO.getTarget())
                        .orderByDesc(Course::getUpdateTime)
        );
        return PageResult.<Course>builder()
                .total(Long.valueOf(list.getTotal()).intValue())
                .rows(list.getRecords())
                .build();
    }

}
